package de.ergin.fuhrparkv2.activities;

import de.ergin.fuhrparkv2.R;
import de.ergin.fuhrparkv2.daten.DatenbankManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class FahrzeugDetails extends Activity {

	protected DatenbankManager mDatabaseHelper;
	protected SQLiteDatabase mDatabase;
	protected Cursor mCarCursor;
	protected TextView mFahrzeugTextView;

	protected int mCarID;
	private final String errorMSG = "Noch keine Wartung Vorhanden";

	@Override
	protected void onResume() {
		super.onResume();
		this.mDatabase = mDatabaseHelper.getReadableDatabase();
		showCarInformationOnActivityScreen();
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.mDatabase.close();
	}

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fahrzeugdetails);

		this.mCarID = getIntent().getIntExtra("CarID", 0);
		this.mDatabaseHelper = new DatenbankManager(this);
		this.mDatabase = this.mDatabaseHelper.getReadableDatabase();

		showCarInformationOnActivityScreen();

	}

	

	public void onButtonClick(View view) {
		switch (view.getId()) {

		case R.id.btn_Service:
			Intent intent = new Intent(this, FahrzeugInspektion.class);
			intent.putExtra("carID", mCarID);
			startActivity(intent);
			break;
		case R.id.btn_CarDelete:
			this.ShowDeleteDialog();
			break;

		case R.id.btn_LastService:
			Intent intent2 = new Intent(this, FahrzeugDiagramm.class);
			intent2.putExtra("carID", mCarID);
			startActivity(intent2);
			break;

		}
	}

	protected void ShowDeleteDialog() {

		AlertDialog.Builder dialog = new AlertDialog.Builder(this);

		dialog.setTitle(getString(R.string.str_wollen_sie_das_fahrzeug_wirklich_loeschen));
		dialog.setPositiveButton(getString(R.string.str_ja),this.GetOnOkClickListener());
		dialog.setNegativeButton(getString(R.string.str_nein),this.GetOnNoClickListener());
		dialog.show();
	}

	private OnClickListener GetOnOkClickListener() {
		OnClickListener result = new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				FahrzeugDetails.this.onOkClick(dialog, which);
			}
		};
		return result;
	}

	private void onOkClick(DialogInterface dialog, int whichButton) {
		try {

			mDatabase.execSQL(DatenbankManager.FAHRZEUG_DELETE, new String[] {""+mCarID });

		} catch (Exception e) {
			throw new NullPointerException(e.toString());
		}
		
		startActivity(new Intent(FahrzeugDetails.this, FahrzeugAuswaehlen.class));
		finish();
	}

	private OnClickListener GetOnNoClickListener() {
		OnClickListener result = new OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				return;
			}
		};

		return result;
	}
	
	private void showCarInformationOnActivityScreen() {
		this.mCarCursor = this.mDatabase.rawQuery(
				DatenbankManager.FAHRZEUGE_SELCET_WARTUNG, new String[] { ""
						+ this.mCarID });

		this.mFahrzeugTextView = (TextView) findViewById(R.id.txt_SelectCar);
		if (this.mCarCursor.getCount() == 0) {
			mFahrzeugTextView.setText(errorMSG);
		} else {
			
			this.mCarCursor.moveToLast();

			this.mFahrzeugTextView.setText(
					 "Die ID des Fahrzeugs ist: "
					+ this.mCarCursor.getInt(this.mCarCursor.getColumnIndex("id"))
					+ "\nLetzte Wartung war am: "
					+ this.mCarCursor.getString(this.mCarCursor.getColumnIndex("datum"))
					+ "\nAktuelle Laufkilometer "
					+ this.mCarCursor.getInt(this.mCarCursor.getColumnIndex("laufkilometer"))
					);
		}
	}
}
